package com.lingz.manager.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

@Slf4j
@Component
public class RoleVersion {

    /**
     * 密钥
     */
    @Value("${DEFAULT_KEY}")
    private String DEFAULT_KEY;

    @Value("${FILE_NAME}")
    private  String FILE_NAME;


    @Value("${ROLE_VERSION}")
    private  String ROLE_VERSION;


    public String getVersion() {
       String path = System.getProperty("user.dir");
        System.out.println("项目路劲"+path);
        path=path+"\\"+ROLE_VERSION;
        System.out.println(path);
        File file = new File(path);
        FileReader reader=null;
        BufferedReader br=null;
        if(file.exists()){
            try {
                reader = new FileReader(path);
                br = new BufferedReader(reader);
                StringBuilder builder = new StringBuilder();
                String s;
                while((s=br.readLine())!=null){
                    builder.append(s);
                }
                String str = builder.toString();
                String decodeStr = decrypt_base64(str,DEFAULT_KEY);
                String[] split = decodeStr.split("\\|\\|");
                for(int i=0;i<split.length;i++){
                    System.out.println(split[i]);
                    return split[0];
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
      return  "";
    }


    public static String decrypt_base64(String base64message, String key) throws Exception {
        String message = new String(Base64.decodeBase64(base64message.getBytes("UTF-8")));
        String result = decrypt(message, key);
        return result;
    }

    public  static String decrypt(String message, String key) throws Exception {

        byte[] bytesrc = convertHexString(message);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte, "UTF-8");
    }

    public  static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }

        return digest;
    }
}
